package com.meihua.shellapp.adapter.personalcenter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.meihua.shellapp.R;
import com.meihua.shellapp.base.BaseBean;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.databean.ReleaseDataBean;
import com.meihua.shellapp.dialog.BaseMessageDialog;
import com.meihua.shellapp.utils.DateUtils;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;

import java.util.List;



public class MyReleaseAdapter extends RecyclerView.Adapter<MyReleaseAdapter.H> {

    private Context mContext;//上下文
    private BaseMessageDialog dialog;

    private List<ReleaseDataBean> mList;//不需要初始化，引用调用

    /**
     * 1.构造
     *
     * @param context 上下文
     */
    public MyReleaseAdapter(Context context, List<ReleaseDataBean> list) {
        mContext = context;
        mList = list;
    }

    /**
     * 2.创建ViewHolder
     *
     * @param parent   根布局
     * @param viewType 类型
     * @return ViewHolder
     */
    @Override
    public H onCreateViewHolder(ViewGroup parent, int viewType) {
        return new H(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_release, null));
    }

    public void setmList(List<ReleaseDataBean> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    /**
     * 3.在绑定ViewHolder之后设置界面
     *
     * @param h        ViewHolder
     * @param position 位置
     */
    @Override
    public void onBindViewHolder(final H h, final int position) {
        //绑定布局
        h.tvArticleTitle.setText(mList.get(position).getTitle());
        h.tvArticleContent.setText(mList.get(position).getContent());
        h.tvReleaseTime.setText(DateUtils.getInstance().getTime(mList.get(position).getCreate_time()));
        h.tveRleaseDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog = new BaseMessageDialog.Builder(mContext)
                        .setMessage("确定要删除吗？")
                        .setClickSure(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                delRelease(position, h.tveRleaseDel);
                                dialog.dismiss();
                            }
                        })
                        .show();

            }
        });
    }

    /**
     * 删除我的发布
     *
     * @param position
     * @param view
     */
    private void delRelease(final int position, final View view) {
        OkGo.<String>post(URL.getUrl().DELRELEASE)
                .params("id", mList.get(position).getId())
                .params("mid", MyApplication.getInstance().getMId())
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        view.setEnabled(false);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        BaseBean bean = MyApplication.getInstance().getBeanForJson(BaseBean.class, response.body());
                        if (bean == null) {
                            return;
                        }
                        if (bean.getCode() == 1) {
                            mList.remove(position);
                            notifyDataSetChanged();
                        }
                        ToastUtils.getInstance().showToast(bean.getMsg());
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        view.setEnabled(true);
                    }
                });
    }

    /**
     * 4.获取数据个数
     *
     * @return
     */
    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();//测试
    }

    /**
     * 5.内部类创建ViewHolder
     */
    public class H extends RecyclerView.ViewHolder {
        TextView tvArticleTitle;
        TextView tvArticleContent;
        TextView tvReleaseTime;
        TextView tveRleaseDel;

        public H(View v) {
            super(v);
            tvArticleTitle = v.findViewById(R.id.tv_article_title);
            tvArticleContent = v.findViewById(R.id.tv_article_content);
            tvReleaseTime = v.findViewById(R.id.tv_release_time);
            tveRleaseDel = v.findViewById(R.id.tv_release_del);
        }
    }
}
